package com.tuanzi.loan.business.service.miniApps.recommend.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsArticle;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsArticleRelations;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsRelation;
import com.tuanzi.loan.business.repository.miniApps.articleLibrary.HealthHeadlineRepository;
import com.tuanzi.loan.business.repository.miniApps.articleLibrary.MiniAppsArticleRelationsRepository;
import com.tuanzi.loan.business.repository.miniApps.articleLibrary.MiniAppsArticleRepository;
import com.tuanzi.loan.business.repository.miniApps.articleLibrary.MiniAppsRelationRepository;
import com.tuanzi.loan.business.repository.miniApps.recommend.HealthHeadlineRecommendRepository;
import com.tuanzi.loan.business.service.miniApps.recommend.HealthHeadlineRecommendService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.miniApps.recommend.HealthHeadlineRecommendRequest;
import com.tuanzi.loan.vo.response.miniApps.recommend.HealthHeadlineRecommendResponse;
import com.tuanzi.loan.vo.view.miniApps.MiniAppsRelationVo;
import com.tuanzi.loan.vo.view.miniApps.recommend.HealthHeadlineRecommendInfoView;
import com.tuanzi.server.loan_base.constant.miniapps.article.ArticleConstant;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class HealthHeadlineRecommendServiceImpl extends BaseAuditServiceImpl<MiniAppsArticleRelations, Long> implements HealthHeadlineRecommendService {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private HealthHeadlineRepository healthHeadlineRepository;
	@Autowired
	private MiniAppsArticleRepository miniAppsArticleRepository;
	@Autowired
	private MiniAppsArticleRelationsRepository miniAppsArticleRelationsRepository;
	@Autowired
	private MiniAppsRelationRepository miniAppsRelationRepository;
	@Autowired
	private HealthHeadlineRecommendRepository headlineRecommendRepository;
	
	public void saveAdvertise(HealthHeadlineRecommendInfoView view) {
		Long articleRelationId = view.getArticleRelationId();
		MiniAppsArticleRelations advertise = null;
		if(articleRelationId == null || articleRelationId == 0) {
			advertise = new MiniAppsArticleRelations();
		} else {
			advertise = miniAppsArticleRelationsRepository.findOne(view.getArticleRelationId());
		}
		
		advertise.setArticleId(view.getArticleId());
		advertise.setOfflineTime(view.getAdvertiseOfflineDate());
		advertise.setOnlineTime(view.getAdvertiseOnlineDate());
		advertise.setRelationId(view.getAdvertiseId());
		advertise.setStatus(Integer.valueOf(view.getAdvertiseStatus()));
		advertise.setWeight(view.getWeight());
		saveOrUpdate(advertise);
		
		// 更新所选小程序
		List<String> typeList = new ArrayList<String>();
		typeList.add(ArticleConstant.ARTICLE_RELATION_TYPE_MINI_APPS);
		List<MiniAppsArticleRelations> miniAppsList = healthHeadlineRepository.selectRelationByArticleIdAndTypeList(advertise.getArticleId(), typeList);
		for(MiniAppsArticleRelations apps : miniAppsList) {
			miniAppsArticleRelationsRepository.delete(apps.getId());
		}
			
		for(String id : view.getSelectedMiniAppsList()) {
			MiniAppsArticleRelations miniApps = new MiniAppsArticleRelations();
			miniApps.setArticleId(view.getArticleId());
			miniApps.setRelationId(Integer.valueOf(id));
			miniAppsArticleRelationsRepository.save(miniApps);
		}
		
	}
	
	public HealthHeadlineRecommendInfoView loadHealthHeadlineInfoView(Long articleRelationId) {
		MiniAppsArticleRelations advertise = miniAppsArticleRelationsRepository.findOne(articleRelationId);
		HealthHeadlineRecommendInfoView view = modelToView(advertise);
		List<MiniAppsRelation> selectedMiniAppsList = healthHeadlineRepository.getMiniAppsListOfArticle(view.getArticleId());
		view.setSelectedMiniAppsList(relationToString(selectedMiniAppsList));
		return view;
	}
	
	public List<MiniAppsArticle> getRecommendArticle() {
		return healthHeadlineRepository.getRecommendArticleList();
	}
	
	private List<String> relationToString(List<MiniAppsRelation> list) {
		List<String> strList = new ArrayList<String>();
		for(MiniAppsRelation model : list) {
			strList.add(String.valueOf(model.getId()));
		}
		return strList;
	}
	
	/**
	 * MiniAppsRelation 数据库实体转换为Vo
	 */
	private List<MiniAppsRelationVo> relationBeanToVo(List<MiniAppsRelation> list) {
		List<MiniAppsRelationVo> volist = new ArrayList<MiniAppsRelationVo>();
		if(list != null) {
			for(MiniAppsRelation model : list) {
				MiniAppsRelationVo vo = new MiniAppsRelationVo();
				BeanUtils.copyProperties(model, vo);
				volist.add(vo);
			}
		}
		return volist;
	}
	
	/**
	 * 数据库实体转换为view Bean
	 * @param model
	 * @return
	 */
	private HealthHeadlineRecommendInfoView modelToView(MiniAppsArticleRelations model) {
		HealthHeadlineRecommendInfoView view = new HealthHeadlineRecommendInfoView();
		view.setArticleRelationId(model.getId());
		view.setAdvertiseId(model.getRelationId());
		view.setAdvertiseOfflineDate(model.getOfflineTime());
		view.setAdvertiseOnlineDate(model.getOnlineTime());
		view.setAdvertiseStatus(String.valueOf(model.getStatus()));
		view.setArticleId(model.getArticleId());
		view.setWeight(model.getWeight());
		return view;
	}
	
	/**
	 * 删除广告位
	 * @param articleRelationId
	 */
	public void deleteAdvertise(Long articleRelationId) {
		miniAppsArticleRelationsRepository.delete(articleRelationId);
	}
	
	/**
	 * 更新广告位状态
	 * @param articleRelationId
	 * @param status
	 */
	public void updateAdvertiseStatus(HealthHeadlineRecommendRequest request ,Integer status) {
		MiniAppsArticleRelations model = miniAppsArticleRelationsRepository.findOne(request.getArticleRelationId());
		model.setStatus(status);
		miniAppsArticleRelationsRepository.save(model);
	}

	public Page<HealthHeadlineRecommendResponse> list(
			PageSearcher<HealthHeadlineRecommendRequest> pageSearcher) {
		Page<HealthHeadlineRecommendResponse> pageList = headlineRecommendRepository.getListPage(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
		return pageList;
	}

	public List<MiniAppsRelationVo> getMiniAppsList() {
		return relationBeanToVo(healthHeadlineRepository.getMiniAppsList());
	}
	
	public List<MiniAppsRelationVo> getAdvertiseList() {
		return relationBeanToVo(healthHeadlineRepository.getAdvertiseList());
	}

	public void cleanCache() {
		healthHeadlineRepository.cleanCache();
	}

	@Override
	protected JPAAuditRepository<MiniAppsArticleRelations, Long> getRepository() {
		return miniAppsArticleRelationsRepository;
	}

	
}















